<template>
  <el-dialog
    :title="!dataForm.id ? 'create' : 'modify'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="paramKey" prop="paramKey">
        <el-input v-model="dataForm.paramKey" placeholder="paramKey"></el-input>
      </el-form-item>
      <el-form-item label="paramValue" prop="paramValue">
        <el-input v-model="dataForm.paramValue" placeholder="paramValue"></el-input>
      </el-form-item>
      <el-form-item label="remark" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">Cancel</el-button>
      <el-button type="primary" @click="dataFormSubmit()">Yes</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          paramKey: '',
          paramValue: '',
          remark: ''
        },
        dataRule: {
          paramKey: [
            { required: true, message: 'Param key name can not null', trigger: 'blur' }
          ],
          paramValue: [
            { required: true, message: 'Param value can not null', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/config/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.paramKey = data.config.paramKey
                this.dataForm.paramValue = data.config.paramValue
                this.dataForm.remark = data.config.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        var _this = this
        _this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            _this.$http({
              url: _this.$http.adornUrl(`/sys/config/${!_this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: _this.$http.adornData({
                'id': _this.dataForm.id || undefined,
                'paramKey': _this.dataForm.paramKey,
                'paramValue': _this.dataForm.paramValue,
                'remark': _this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                _this.$message.success('success', 1, function () {
                  _this.visible = false
                  _this.$emit('refreshDataList')
                })
              } else {
                _this.$message.error(data.msg, 1)
              }
            })
          }
        })
      }
    }
  }
</script>
